<template>
    <div id="changePwd">
        <div class="form_box">
            <div>您的手机号为：</div>
            <el-divider></el-divider>
            <div>{{requestData.phone}}</div>
            <el-divider></el-divider>
            <el-form :model="form">
                <el-form-item label="请输入完整手机号" :label-width="'160px'">
                    <el-input v-model="phone"></el-input>
                </el-form-item>

                <el-form-item label="" :label-width="'160px'">
                    <el-button type="primary" @click="sendVerifyCode" :disabled="countDownTime!==0">
                        {{countDownTime===0?'获取验证码':`${countDownTime}秒后重新获取`}}
                    </el-button>
                </el-form-item>
            </el-form>

            <el-form :model="verifyForm" v-show="countDownTime>0">
                <el-form-item label="验证码" :label-width="'80px'">
                    <el-input v-model="verifyForm.verifyCode"></el-input>
                </el-form-item>

                <el-form-item label="" :label-width="'80px'" v-show="false">
                    <el-input v-model="verifyForm.verifyToken"></el-input>
                </el-form-item>
                <el-button type="primary" @click="verifySms">确认</el-button>
            </el-form>
        </div>

        <el-dialog
                title="修改密码"
                :visible.sync="editDialogVisible"
                width="60%"
                center>

            <el-form :model="changePwdForm" status-icon :rules="rules" ref="changePwdForm" label-width="100px" class="demo-ruleForm">

                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="changePwdForm.password" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="确认密码" prop="confirmPassword">
                    <el-input type="password" v-model="confirmPassword" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="" prop="confirmPassword" v-show="false">
                    <el-input type="password" v-model="changePwdForm.verifiedToken" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" :aria-disabled="true" @click="changePassword">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "ChangePwd",


        data(){
            let validateConfirm = (rule, value, callback) => {
                    if (this.confirmPassword === '') {
                        callback(new Error('请再次输入密码'));
                    } else if (this.confirmPassword !== this.changePwdForm.password) {
                        callback(new Error('两次输入密码不一致!'));
                    } else {
                        callback();
                    }
                };

            return{
                btnStatus: true,
                countDownTime: 0,
                rules: {
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, message: '密码最小6位', trigger: 'blur' }
                    ],
                    confirmPassword: [
                        { validator: validateConfirm, trigger: 'blur' }
                    ]
                },
                editDialogVisible: false,
                phone: null,
                showInput: false,
                form:{},

                requestData:{

                },
                verifyForm:{
                    verifyCode: null,
                    verifyToken:null,
                },
                changePwdForm:{
                    verifiedToken: null,
                    password: null
                },
                confirmPassword: null,
                verifiedForm:{}
            }
        },
        methods:{

            setCountDown(){
                let item = sessionStorage.getItem('time');
                if (item === null || item === undefined){
                    this.countDownTime = 0;
                }else {
                    this.countDownTime = item;
                }
                //this.countDownTime = item === null || item === undefined ? 0 : item;
            },
            codeCountDown(){
                let time = setTimeout(() => {
                    let sessionTime = sessionStorage.getItem('time');
                    if (sessionTime !== null && sessionTime !== undefined){
                        if (sessionTime>0){
                            sessionTime--;
                            sessionStorage.setItem('time',sessionTime);
                            this.codeCountDown(sessionTime);
                        }else {
                            sessionStorage.removeItem('time');
                            clearTimeout(time);
                        }
                    }
                    this.setCountDown();
                }, 1000)
            },
            async getPerson() {
                let resp = await this.$http.get("/sysUser/person");
                let data = resp.data;
                if (data.code === 200) {
                    this.requestData = data.data;
                } else {
                    this.$message.error(data.info);
                }
            },

            async sendVerifyCode() {
                let sessionTime = sessionStorage.getItem('time');
                if (sessionTime === null || sessionTime === undefined){
                    sessionStorage.setItem('time',60);
                    this.codeCountDown();
                    let resp = await this.$http.post("/sysUser/sendPhoneVerify?phone="+this.phone);
                    let data = resp.data;
                    if (data.code === 200) {
                        this.$message.success("发送成功");
                        this.showInput = true;
                        this.verifyForm.verifyToken = data.data.verifyToken;
                    } else {
                        this.$message.error(data.info);
                    }
                }
            },

            async verifySms() {
                let resp = await this.$http.post("/sysUser/verifySms", this.verifyForm);
                let data = resp.data;
                if (data.code === 200) {
                    this.$message.success("验证成功,请尽快修改密码");
                    this.changePwdForm.verifiedToken = data.data.verifiedToken;
                    this.editDialogVisible = true;
                }else {
                    this.$message.error(data.info);
                }
            },

            async changePassword() {
                this.$refs.changePwdForm.validate((valid) => {
                    if (valid) {
                        this.$http.post("/sysUser/changePassword",this.changePwdForm).then(resp => {
                            let data = resp.data;
                            if (data.code === 200) {
                                //密码修改成功
                                this.$message.success(data.info);
                                sessionStorage.clear();
                                this.$router.push("/login");
                            } else {
                                this.$message.error(data.info);
                            }
                        });
                    } else {
                        return false;
                    }
                });

            },
        },
        created() {
            this.getPerson();
            this.codeCountDown();
        }
    }
</script>

<style scoped>
    #changePwd{
        width: 100%;
    }
    .form_box{
        width: 50%;
        margin-left: 25%;
    }
</style>
